//
//  AppGoodsDetailController.m
//  gungunchou
//
//  Created by lyzbmacos3 on 2018/12/6.
//  Copyright © 2018年 xskj. All rights reserved.
//

#import "AppGoodsDetailController.h"
#import "GoodsDesCell.h"
#import "XHSegmentControl.h"
#import "LJWebKitSupport.h"

@interface AppGoodsDetailController () <UITableViewDelegate,UITableViewDataSource,XHSegmentControlDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak, nonatomic) IBOutlet UILabel *scoreLbl;
@property (weak, nonatomic) IBOutlet UIView *scroeV;
@property (strong, nonatomic) WKWebView *wkWeb;
@property (nonatomic, strong) XHSegmentControl *segCon;

@property (nonatomic, assign) BOOL isLoadWeb;
@property (nonatomic, strong) ProductModel *dataModel;
@end

@implementation AppGoodsDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configViews];
    
    self.navigationItem.title = @"商品详情";
    
    [self.table registerNib:[UINib nibWithNibName:@"GoodsDesCell" bundle:nil] forCellReuseIdentifier:@"GoodsDesCell"];
    [self fetchDetailData];
}

- (void)configViews
{
    _segCon = [[XHSegmentControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    WS(weakSelf)
    _segCon.delegate = weakSelf;
    _segCon.titles = @[@"概述",@"详情"];
    [self.view addSubview:_segCon];
    [_segCon load];
    
    self.wkWeb = [[WKWebView alloc] initWithFrame:CGRectZero configuration:[LJWebKitSupport sharedSupport].webViewConfiguration];
    self.wkWeb.scrollView.showsVerticalScrollIndicator = NO;
    self.wkWeb.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.wkWeb];
    [self.wkWeb mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.segCon.mas_bottom);
        make.bottom.equalTo(self.scroeV.mas_top);
    }];
    self.wkWeb.hidden = YES;
}

- (void)xhSegmentSelectAtIndex:(NSInteger)index animation:(BOOL)animation
{
    if (index==1) {
        self.wkWeb.hidden = NO;
//        if (!self.isLoadWeb) {
//            self.isLoadWeb = YES;
//            //
//        }
    }
    else self.wkWeb.hidden = YES;
    
}


- (void)fetchDetailData
{
    NSDictionary *parm = [NSDictionary dictionaryWithObjectsAndKeys:self.model.ID,@"ID", nil];
    [MBProgressHUD showHUDAddedTo:KWindow animated:YES];
    [[CGNetManage shared] httpGetGoodsDetailWithParams:parm Completed:^(NSDictionary *data) {
        [MBProgressHUD hideHUDForView:KWindow animated:YES];
        
        self.dataModel = [ProductModel mj_objectWithKeyValues:data];
        if (!self.dataModel) {
            self.dataModel = self.model;
        }
        
        
        [self.wkWeb loadHTMLString:self.dataModel.Desc baseURL:nil];
        
        id imgs = [data objectForKey:@"Images"];
        if (imgs) {
            if ([imgs isKindOfClass:NSString.class]) {
                NSString *theImgs = (NSString *)imgs;
                NSArray *tempArr = [theImgs componentsSeparatedByString:@";"];
                self.dataModel.Images = tempArr;
            }
            else {
                
            }
        }
        
        self.scoreLbl.text = [NSString stringWithFormat:@"积分：%@", self.dataModel.DiscountPrice];
        
        [self.table reloadData];
    } fail:^(NSError *err) {
        [MBProgressHUD hideHUDForView:KWindow animated:YES];
    }];
}

- (IBAction)buyClick:(id)sender {
    if (Usr_Is_L) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"511",@"UserID",self.dataModel.ID,@"ProID",@"1",@"BuyNum", nil];
        [[CGNetManage shared] httpAddCart:dic Completed:^(NSDictionary *response) {
            NSNumber *state = [response objectForKey:@"State"];
            NSString *msg = [response objectForKey:@"Msg"];
//            if (msg.length>0) {
//                [LICGToast showWithText:msg];
//            }
            if (state.integerValue == 1) {//成功
                [LICGToast showWithText:@"添加至购物车成功!"];
            }
            
        } fail:^(NSError *err) {
            [LICGToast showWithText:@"操作失败!"];
        }];
    }
    else {
        [LICGToast showWithText:@"请先登录!"];
    }
}

#pragma mark - table delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsDesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsDesCell"];
    if (self.dataModel) {
        cell.model = self.dataModel;
    }
    
    return cell;
}

@end
